import 'dart:async';

import 'package:flutter/material.dart';

import '../../utils/http.dart';
import '../../utils/toast.dart';
import '../../utils/token.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  // 3. 提交登录信息
  void _submit() async {
    // 3.1 校验账号
    if (_usernameController.text.isEmpty) {
      return ToastUtil.showError('请输入账号');
    }

    //正则6-8位字母或数字
    RegExp regExp = RegExp(r'^[a-zA-Z0-9]{6,8}$');
    if (!regExp.hasMatch(_usernameController.text)) {
      return ToastUtil.showError('请输入正确的账号');
    }

    // 3.2 校验密码
    if (_passwordController.text.isEmpty) {
      return ToastUtil.showError('请输入密码');
    }

    //正则6-8位字母或数字
    RegExp regExp1 = RegExp(r'^[a-zA-Z0-9]{6,8}$');
    if (!regExp1.hasMatch(_passwordController.text)) {
      return ToastUtil.showError('请输入正确的密码');
    }

    // 3.3 调用登录接口
    var res = await http.post('/driver/login/account', {
      'account': _usernameController.text,
      'password': _passwordController.text,
    });
    if (res['code'] != 200) return ToastUtil.showError('登陆失败！');

    // 3.4 存储token
    TokenManager().saveToken(res['data']);
    Navigator.pushReplacementNamed(context, '/home');
    ToastUtil.showSuccess('登录成功！');
  }

  @override
  Widget build(BuildContext context) {
    bool isShow = true;
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: const Text(
            '神领快递',
            style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
          ),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: Container(
            padding: const EdgeInsets.all(20),
            child:
                Column(mainAxisAlignment: MainAxisAlignment.start, children: [
              const SizedBox(height: 50),
              const Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('账号登录',
                      style:
                          TextStyle(fontSize: 25, fontWeight: FontWeight.bold)),
                  Text(
                    '手机号登录 ',
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.red,
                    ),
                  )
                ],
              ),
              const SizedBox(height: 10),
              const SizedBox(height: 30),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: TextField(
                      controller: _usernameController,
                      keyboardType: TextInputType.name,
                      style: const TextStyle(fontSize: 16),
                      decoration: InputDecoration(
                        hintText: '请输入账号',
                        hintStyle: const TextStyle(color: Colors.grey),
                      ),
                    ),
                  )
                ],
              ),
              const SizedBox(height: 8),
              TextField(
                controller: _passwordController,
                keyboardType: TextInputType.name,
                style: const TextStyle(fontSize: 16),
                obscureText: isShow,
                decoration: InputDecoration(
                  hintText: '请输入密码',
                  hintStyle: const TextStyle(color: Colors.grey),
                  suffixIcon: IconButton(
                    icon: Icon(Icons.close),
                    onPressed: () {
                      _passwordController.clear();
                    },
                  ),
                ),
              ),
              const SizedBox(height: 8),
              const SizedBox(height: 50),
              Row(children: [
                Expanded(
                    child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color.fromARGB(255, 255, 0, 0),
                    minimumSize: const Size(90, 50),
                  ),
                  onPressed: () {
                    _submit();
                  },
                  child: const Text('登录',
                      style: TextStyle(color: Colors.white, fontSize: 20)),
                ))
              ])
            ])));
  }
}
